//
//  SalesViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/6/2.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "SalesViewController.h"
#import "SalesTableViewCell.h"
#import "ApplySalesViewController.h"
#import "SalesDetailViewController.h"
@interface SalesViewController ()<UITableViewDelegate ,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *itemArray;
@property (nonatomic ,strong) NSDictionary *dataDic;

@end

@implementation SalesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"申请售后");
    [self loadShopDetail];
    self.itemArray=[NSMutableArray array];
    [self.view addSubview:[self tableView]];
    // Do any additional setup after loading the view.
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.separatorColor=SeparatorColor;
        _tableView.separatorInset=UIEdgeInsetsMake(0, 10, 0, 10);
        _tableView.backgroundColor=Gray_Color;
        [_tableView registerNib:[UINib nibWithNibName:@"SalesTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SalesTableViewCell"];
    }
    return _tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary*dic=self.itemArray[indexPath.row];
    SalesTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"SalesTableViewCell"];
    [cell.shopImag sd_setImageWithURL:[NSURL URLWithString:dic[@"imageUrl"]]];
    cell.shopName.text=dic[@"spuName"];
    cell.shopStyple.text=dic[@"saleName"];
    cell.shopNum.text=[NSString stringWithFormat:@"× %@",dic[@"quantity"]];
    cell.SalesClick.tag=indexPath.row;
    [cell.SalesClick addTarget:self action:@selector(SalesClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 56;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]init];
    view.backgroundColor=[UIColor whiteColor];
    UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, MainW-20, 15)];
    lable.text=[NSString stringWithFormat:@"%@:%@",MyString(@"订单号"),self.modle.orderSn];
    lable.font=[UIFont systemFontOfSize:14];
    lable.textColor=TextColor3;
    [view addSubview:lable];
    UILabel *timelab=[[UILabel alloc]initWithFrame:CGRectMake(10, lable.bsg_bottom+10, MainW-20, 15)];
    NSString *time=[self timeWithTimeIntervalString:self.modle.createdTime];
    timelab.text=[NSString stringWithFormat:@"%@:%@",MyString(@"下单时间"),time];
    timelab.font=[UIFont systemFontOfSize:14];
    timelab.textColor=TextColor3;
    [view addSubview:timelab];
    return view;
}

-(void)SalesClick:(UIButton *)sender{
    NSMutableDictionary*dic=self.itemArray[sender.tag];
    ApplySalesViewController *vc=[[ApplySalesViewController alloc]init];
    vc.goodsDic=dic;
    vc.dataDic=self.dataDic;
    [self.navigationController pushViewController:vc animated:YES];
//    if ([sender.titleLabel.text isEqualToString:@"申请售后"]) {
//        switch (sender.tag) {
//            case 0:
//            {
//                ApplySalesViewController *vc=[[ApplySalesViewController alloc]init];
//                vc.goodsDic=dic;
//                [self.navigationController pushViewController:vc animated:YES];
//            }
//                break;
//            case 1:
//            {
//                SalesDetailViewController *vc=[[SalesDetailViewController alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
//            }
//                break;
//
//            default:
//                break;
//        }
//        
//        
//        NSLog(@"售后");
//    }
}
-(void)loadShopDetail{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"orderSn"]=self.modle.orderSn;
    [self doHttpRequestWithApi:API_ORDERDETAIL_LIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.itemArray=dic[@"data"][@"items"];
        self.dataDic=dic[@"data"];
        [self.tableView reloadData];
        NSLog(@"你的数据呢  %@",self.itemArray);
    }];

}

@end
